import socket from "@/socket";

const state = {
    dynamic: [],
    dynamic_G: [],
    userInfo: ''
}

const mutations = {
    upDynamic(state, data) {
        state.dynamic = data;
    },
    pushDynamic_G(state, data) {
        state.dynamic_G.push(data);
    },
    upUserInfo(state, data) {
        state.userInfo = data;
    },
    setDynamic_like(state, { index, data }) {
        let { dynamic } = state;
        dynamic.at(index).liked_users.push(data);
    },
    setDynamic_comment(state, { index, data }) {
        let { dynamic } = state;
        dynamic.at(index).comment.push(data);
    },
    setDynamic_G_like(state, { index, data }) {
        let { dynamic_G } = state;
        dynamic_G.at(index).liked_users.push(data);
    },
    setDynamic_G_comment(state, { index, data }) {
        let { dynamic_G } = state;
        dynamic_G.at(index).comment.push(data);
    },
}

const actions = {
    getDynamic(context, data) {
        return new Promise((resolve, reject) => {
            socket.emit('get_dynamic', data, res => resolve(res));
        })
    },
    handleLike_dynamic(context, data) {
        return new Promise((resolve, reject) => {
            socket.emit('like_dynamic', data, res => resolve(res));
        })
    },
    handleSend_comment_dynamic(context, data) {
        return new Promise((resolve, reject) => {
            socket.emit('sent_comment', data, res => resolve(res));
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}